<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//站点相关信息
class Article extends MY_Controller {
	public function __construct(){
		parent::__construct();
		$this->sess_checked();
		$this->load->model('info_m');
		$this->load->model('column_m');
		$this->load->model('special_m');
		$this->load->model('specialContentType_m');
		$this->load->model('student_m');
		$this->load->model('associates_m');
	}
	//文章列表
	public function lists(){
		//获取栏目
		$columnselected=$this->column_m->getColumnselected();
		$this->assign('columnselected',$columnselected);

		$where=array();
		if(isset($_POST['title']) && $_POST['title']!=''){
			$where['A.title']=array('like',$_POST['title']);
		}
		if(isset($_POST['colid'])){
			$where['A.colid']=array('=',$_POST['colid']);
		}
		$where['A.delflag']=array('=',0);
		$field='A.*,B.name as sitesname,C.name as columnname,D.displayname';
		if(isset($_POST['sortvalue'])){//排序
			$sortvalue=explode('_',$_POST['sortvalue']);
			if($sortvalue[1]==1){$desc=" desc";}else{$desc=" asc";}
			$order=$sortvalue[0].$desc;
			$order_default=$_POST['sortvalue'];
		}else{
			$order='addtime desc';
			$order_default='addtime_1';
		}
		$this->assign('sortvalue',$order_default);
		$arr=$this->info_m->ajaxall($where,$field,$order);
		//echo $this->db->last_query();
		$this->assign('list',$arr['list']);
		if(isset($_POST['ajaxs'])){
			$htmlcontent=$this->fetch('article/articlelist.html');
			$arr['list']=$htmlcontent;
			 ajax_return(1,'',$arr);
		}
		$this->assign('pageshow',$arr['pageshow']);
		$this->display('article/index.html');
	}
	//新建文章
	public function add(){
		if(isset($_POST['specialcontenttype'])){//根据专题获取专题内容分类专题
			if(!isset($_POST['specialid'])){
				ajax_return(0,'参数错误');
			}
			$specialid=$_POST['specialid'];
			$specialcontent_where['specialid']=array('=',$specialid);
			$specialcontentlist=$this->specialContentType_m->selall($specialcontent_where,'id,ctypename');
			if(!$specialcontentlist){ajax_return(0,'不存在专题内容分类');}
			ajax_return(1,'',$specialcontentlist);
		}elseif(isset($_POST['ajaxs'])){//添加文章
			$data=form_ajaxs($_POST['formjquery']);
			$this->article_data($data);
			if($infoid=$this->info_m->add($data)){
				$this->adminuserRecord_m->add(1,1,'添加文章infoid为'.$infoid);
				ajax_return(1,'添加文章成功');
			}else{
				$this->adminuserRecord_m->add(1,2,'添加文章');
				ajax_return(0,'添加失败');
			}
		}else{//默认显示添加页面
			//获取权限栏目
			$columnselected=$this->column_m->getColumnselected();
			$this->assign('columnselected',$columnselected);
			//获取所有专题
			$special_where['status']=array('=',1);
			$speciallist=$this->special_m->sels($special_where,'id,name','id desc');
			$this->assign('speciallist',$speciallist);
			
			$this->display('article/add.html');
		}
	}
	//编辑文章
	public function edit(){
		if(isset($_POST['recommendflag_ajaxs'])){//推荐修改
			$where['infoid']=array('=',$_POST['infoid']);
			$data['recommendflag']=$_POST['recommendflag'];
			if($_POST['recommendflag']==1){$msg='是';}else{$msg='否';}
			if($this->info_m->edit($where,$data)!==false){
				$this->adminuserRecord_m->add(2,1,'文章infoid为'.$_POST['infoid']."推荐状态改为".$msg);
				ajax_return(1,"修改推荐成功");
			}else{
				$this->adminuserRecord_m->add(2,2,'文章infoid为'.$_POST['infoid']."推荐状态");
				ajax_return(0,'修改推荐失败');
			}
		}elseif(isset($_POST['headflag_ajaxs'])){//置顶修改
			$where['infoid']=array('=',$_POST['infoid']);
			$data['headflag']=$_POST['headflag'];
			if($_POST['headflag']==1){$msg='是';}else{$msg='否';}
			if($this->info_m->edit($where,$data)!==false){
				$this->adminuserRecord_m->add(2,1,'文章infoid为'.$_POST['infoid']."置顶状态改为".$msg);
				ajax_return(1,"修改置顶成功");
			}else{
				$this->adminuserRecord_m->add(2,2,'文章infoid为'.$_POST['infoid']."置顶状态");
				ajax_return(0,'修改置顶失败');
			}
		}elseif(isset($_POST['verify_ajaxs'])){//审核修改
			$where['infoid']=array('=',$_POST['infoid']);
			$data['verify']=$_POST['verify'];
			if($_POST['verify']==1){$msg='已审核';}else{$msg='取消审核';}
			if($this->info_m->edit($where,$data)!==false){
				$this->adminuserRecord_m->add(2,1,'文章infoid为'.$_POST['infoid']."审核状态改为".$msg);
				ajax_return(1,"修改审核成功");
			}else{
				$this->adminuserRecord_m->add(2,2,'文章infoid为'.$_POST['infoid']."审核状态");
				ajax_return(0,'修改审核失败');
			}
		}elseif(isset($_POST['seqedit'])){//更新排序
			$where['infoid']=array('=',$_POST['infoid']);
			$data['seq']=$_POST['orderid'];
			if($this->info_m->edit($where,$data)!==false){
				$this->adminuserRecord_m->add(2,1,'文章infoid为'.$_POST['infoid']."修改排序为".$_POST['orderid']);
				ajax_return(1,"修改排序成功");
			}else{
				$this->adminuserRecord_m->add(2,2,'文章infoid为'.$_POST['infoid']."修改排序为".$_POST['orderid']);
				ajax_return(0,'修改排序失败');
			}
		}elseif(isset($_POST['editAll_ajaxs'])){//批量修改推荐 置顶 审核
			$type=$_POST['type'];
			$infoidarr=explode(',',$_POST['infostr']);
			$where['infoid']=array('in',$infoidarr);
			$data[$type]=$_POST['value'];
			if($_POST['value']==1){$status='开启';}else{$status='关闭';}
			if($type=='recommendflag'){
				$msg="文章infoid在".$_POST['infostr'].'中推荐'.$status;
			}elseif($type=='headflag'){
				$msg="文章infoid在".$_POST['infostr'].'中置顶'.$status;
			}elseif($type=='verify'){
				$msg="文章infoid在".$_POST['infostr'].'中审核'.$status;
			}else{
				ajax_return(0,'参数错误');
			}
			if($this->info_m->edit($where,$data)!==false){
				$this->adminuserRecord_m->add(2,1,$msg);
				ajax_return(1,"修改成功");
			}else{
				$this->adminuserRecord_m->add(2,2,$msg);
				ajax_return(0,'修改失败');
			}
		}elseif(isset($_POST['ajaxs'])){
			$data=form_ajaxs($_POST['formjquery']);
			if(!isset($data['title']) || $data['title']==''){ajax_return(0,'标题不能为空');}
			if(!isset($data['content']) || $data['content']==''){ajax_return(0,'内容不能为空');}
			//更新后的其他文章信息

			$data=$this->info_m->return_edit($data);
			$where['infoid']=array('=',$data['infoid']);
			if($this->info_m->edit($where,$data)!==false){
				$this->adminuserRecord_m->add(1,1,'修改文章infoid为'.$data['infoid']);
				ajax_return(1,'修改文章成功');
			}else{
				$this->adminuserRecord_m->add(1,2,'修改文章infoid'.$data['infoid']);
				ajax_return(0,'修改失败');
			}

		}elseif(isset($_GET['list']) && isset($_GET['infoid'])){//文章详情编辑
			//获取文章内容
			$where['infoid']=array('=',$_GET['infoid']);
			$list=$this->info_m->sel($where);
			if(!$list){Js::alert('不存在文章');}
			$list['title']=str_safe($list['title'],'noescape');
			$list['intro']=str_safe($list['intro'],'noescape');
			$list['mediafile']=str_safe($list['mediafile'],'noescape');
			$list['content']=str_safe($list['content'],'noescape');
			$this->assign('list',$list);
			//获取权限栏目
			$columnselected=$this->column_m->getColumnselected($list['colid']);
			$this->assign('columnselected',$columnselected);

			$this->assign('list',$list);
			$this->display('article/edit.html');
		}
	}
	//删除到回收站
	public function del(){
		if(isset($_POST['infoid']) && isset($_POST['recycle'])){
			$where['infoid']=array('=',$_POST['infoid']);
			$data['delflag']=1;
			if($this->info_m->edit($where,$data)!==false){
				$this->adminuserRecord_m->add(3,1,'文章infoid为'.$_POST['infoid']."删除到回收站");
				ajax_return(1,"删除到回收站成功");
			}else{
				$this->adminuserRecord_m->add(3,2,'文章infoid为'.$_POST['infoid']."删除到回收站");
				ajax_return(0,'删除到回收站失败');
			}
		}else{
			ajax_return(0,'参数错误');
		}
	}
	//文章添加验证
	private function article_data($data){
		if(!isset($data['colid']) || $data['colid']==''){ajax_return(0,'请选择栏目');}
		if(!isset($data['title']) || $data['title']==''){ajax_return(0,'标题不能为空');}
		if(!isset($data['content']) || $data['content']==''){ajax_return(0,'内容不能为空');}
	}

	
}